/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.I_ProductAuxiliar;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.KeyBuilder;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import com.openbravo.pos.bean.ProductExt;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductAuxiliar;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class ProductAuxiliarDAO  extends ModifiedLogDAO<ProductAuxiliar>{

	ProductDAO productDAO;
	
    public ProductAuxiliarDAO(I_Session s,ProductDAO productDAO) {
        super(s);
        this.productDAO=productDAO;
    }

    
    public ProductAuxiliarDAO(I_Session s) {
    	super(s);
	}


	@Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                "PRODUCTS_COM",
                new Field[]{
                     new Field(I_ProductAuxiliar.ID, Datas.STRING, Formats.STRING),
                     new Field(I_ProductAuxiliar.PRODUCT1, Datas.STRING, Formats.STRING,Product.class),
                     new Field(I_ProductAuxiliar.PRODUCT2, Datas.STRING, Formats.STRING),
                     new Field(I_ProductAuxiliar.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)
                   },
                   new int[]{0}
                );
    }

    @Override
    public void writeInsertValues(DataWrite dp, ProductAuxiliar obj) throws BasicException {
          dp.setString(1,obj.getId());
          dp.setString(2, obj.getProduct1());
          dp.setString(3,obj.getProduct2());
          dp.setTimestamp(4, obj.getLastModified());
    }

    @Override
    public Class getSuportClass() {
        return ProductAuxiliar.class;
    }

    public ProductAuxiliar readValues(DataRead dr,ProductAuxiliar obj) throws BasicException {
         if(obj==null)
        	 obj=new ProductAuxiliar();
         obj.setId(dr.getString(1));
         obj.setProduct1(dr.getString(2));
         obj.setProduct2(dr.getString(3));
         obj.setLastModified(dr.getTimestamp(4));
         if(productDAO!=null)
           obj.setProduct(productDAO.find(KeyBuilder.getKey(obj.getProduct2())));
         return obj;
    }

    @Override
    public List<ProductAuxiliar> list(Object filter) throws BasicException {
        return new PreparedSentence(s,
                this.getTableDefinition().getListSQL()+" WHERE PRODUCT = ?",
                SerializerWriteString.INSTANCE, this).list(filter);
    }

    @Override
    public Class transClass(Class in) {
        if(in==ProductExt.class)
            return Product.class;
        return super.transClass(in);
    }
}
